<template>
  <div class="body-container layout-padding">
    <el-form label-width="120px" style="margin-bottom: 10px">
      <el-row :gutter="10">
        <el-col :span="4">
          <el-form-item label="仓库名称">
            <el-select filterable clearable v-model="state.sd.cwhname" placeholder="仓库名称">
              <el-option  v-for="item in state.whList" :key="item.cwhcode" :label="item.cwhname" :value="item.cwhname" />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item label="存货名称">
            <el-input v-model="state.sd.cinvname" placeholder="存货名称"/>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="现存量>0">
            <el-checkbox v-model="state.sd.iquantityflag" />
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="未盘点">
            <el-checkbox v-model="state.sd.npdflag" />
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="已盘点">
            <el-checkbox v-model="state.sd.pdflag" />
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label="差异">
            <el-checkbox v-model="state.sd.czquantityflag" />
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-button size="default" type="primary" class="ml5" @click="searchSnapshotRecord">
            <el-icon><ele-Search /></el-icon>查询
          </el-button>
          <el-button size="default" type="primary" class="ml5" @click="exportCurrentStore" :loading="state.exportBtnLoading">
            <el-icon><ele-Download /></el-icon>导出
          </el-button>
        </el-col>
      </el-row>
    </el-form>

    <el-table :data="state.csObj.list" border style="width: 100%">
      <el-table-column label="仓库名称" prop="cwhname"></el-table-column>
      <el-table-column label="存货编码" prop="cinvcode"></el-table-column>
      <el-table-column label="存货名称" prop="cinvname"></el-table-column>
      <el-table-column label="规格型号" prop="cinvstd"></el-table-column>
<!--      <el-table-column label="批号" prop="cbatch"></el-table-column>-->
      <el-table-column label="单位名称" prop="ccomunitname"></el-table-column>
      <el-table-column label="盘点时现存量"> <!-- 2024-09-19 陈宾修改 之前是显示iquantity字段， 现在废弃不用了-->
        <template v-slot="scope">
          <span v-if="scope.row.pdquantity">{{scope.row.pdquantity - scope.row.czquantity}}</span>
        </template>
      </el-table-column>
      <el-table-column label="盘点数量" prop="pdquantity"></el-table-column>
      <el-table-column label="盘亏盘盈" prop="czquantity"></el-table-column>
      <el-table-column label="盘点人员" prop="pduser"></el-table-column>
      <el-table-column label="盘点时间">
        <template v-slot="scope">
          <DateFormat :value="scope.row.lasttbdate" :format="'YYYY-MM-DD HH:mm'" v-if="scope.row.lasttbdate"></DateFormat>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination class="mt15" background layout="total,sizes, prev, pager, next"
                   @current-change="loadSnapshotRecord" @size-change="loadSnapshotRecord" :total="state.csObj.total"
                   v-model:current-page="state.sd.pageNum" v-model:page-size="state.sd.pageSize" :page-sizes="state.sd.pageSizes">
    </el-pagination>
  </div>
</template>

<script setup name="currentStoreIndex">
import request from '/@/utils/request';
import { ElMessage } from 'element-plus';
import mittBus from '/@/utils/mitt';
import PermissionType from "/@/utils/PermissionType";
import {reactive, ref, onMounted, onUnmounted, defineProps, defineAsyncComponent} from "vue";
import { useRouter, useRoute } from "vue-router";
const router = useRouter();
const route = useRoute();

const baseURL= import.meta.env.VITE_API_URL;
const state = reactive({
  permissionType:PermissionType,
  sd: {
    ssId : undefined,
    pageNum: 1,
    pageSize: 100,
    pageSizes: [50, 100, 150, 200],
    cwhname:'',
    cinvcode:'',
    cinvname:'',
    iquantityflag:true,
    pdflag:false,
    czquantityflag:false,
    npdflag:false,
  },
  csObj: {},
  whList : [],
  exportBtnLoading : false,
})
const searchSnapshotRecord = ()=>{
  state.sd.pageNum = 1;
  loadSnapshotRecord();
}
const loadSnapshotRecord = ()=>{
  request({
    url: '/cssnapshot/listcssnapshotrecord',
    data: state.sd
  }).then(res => {
    const { data } = res
    state.csObj = data
  })
}
const loadWhList = ()=>{
  request({
    url: '/cssnapshot/listrecordwh',
    data: {ssId : state.sd.ssId }
  }).then(res => {
    const { data } = res
    state.whList = data
  })
}
const exportCurrentStore = ()=>{
  state.exportBtnLoading = true;
  request({
    url: '/cssnapshot/exportcssnapshotrecord',
    data: state.sd
  }).then(res => {
    state.exportBtnLoading = false;
    const { data } = res
    const url = baseURL + "/profile/temp/" + data
    let a = document.createElement('a')
    a.href = url;
    a.click();
  })
}
onMounted(()=>{
  state.sd.ssId = parseInt(route.params.id)
  loadWhList();
  loadSnapshotRecord();
})
onUnmounted(()=>{

})
</script>
<style scoped>
</style>
